package com.qf.interceptor;


import com.qf.entity.Admin;
import com.qf.exception.CustomerException;
import com.qf.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义的登陆拦截器
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {

    @Autowired
    private RedisUtil redisUtil;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //获取请求头
        String token = request.getHeader("Authorization");
        System.out.println("token:"+token);
        //判断token
        if (token == null || token.isEmpty()){
            throw new CustomerException("403","无效的token，请重新登录");
        }
        //判断token是否存在
        Admin user = (Admin) redisUtil.getCacheObject(token);
        if (user == null){
            throw new CustomerException("403","token失效，请重新登录");
        }
        return true;
    }
}
